package com.zndroid.netmonitor;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.zndroid.netmonitor.monitor.IMPL;
import com.zndroid.netmonitor.monitor.INetChangeCallBack;
import com.zndroid.netmonitor.monitor.INetSpeedChangeCallBack;
import com.zndroid.netmonitor.monitor.ISpeed;
import com.zndroid.netmonitor.monitor.impl.MonitorImpl;
import com.zndroid.netmonitor.monitor.impl.MonitorImplAPI21;
import com.zndroid.netmonitor.monitor.impl.MonitorImplAPI23;
import com.zndroid.netmonitor.monitor.impl.MonitorImplAPI26;
import com.zndroid.netmonitor.monitor.impl.MonitorSpeedImpl;
import com.zndroid.netmonitor.monitor.impl.MonitorSpeedImplAPI24;
import com.zndroid.netmonitor.monitor.Util;

public class NetworkMonitor {
    private IMPL IMPL;
    private ISpeed ISPEED;

    public void startMonitor(@NonNull Context context, @NonNull INetChangeCallBack netChangeCallBack) {
        if (null != IMPL) return;//保留一个实例

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            IMPL = new MonitorImplAPI26(netChangeCallBack);
        }
        else
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            IMPL = new MonitorImplAPI23(netChangeCallBack);
        }
        else
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            IMPL = new MonitorImplAPI21(netChangeCallBack);
        }
        else {
            IMPL = new MonitorImpl(netChangeCallBack);
        }

        IMPL.startMonitor(context);
    }

    public void stopMonitor() {
        if (null == IMPL) return;

        IMPL.stopMonitor();
        IMPL = null;
    }

    public void startSpeedMonitor(@NonNull Context context, @NonNull INetSpeedChangeCallBack callBack) {
        startSpeedMonitor(context, 2, callBack);
    }

    public void startSpeedMonitor(@NonNull Context context, int periodSeconds, @NonNull INetSpeedChangeCallBack callBack) {
        if (ISPEED != null) return;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ISPEED = new MonitorSpeedImplAPI24(periodSeconds, callBack);
        } else {
            ISPEED = new MonitorSpeedImpl(periodSeconds, callBack);
        }
        ISPEED.startMonitorSpeed(context);
    }

    public void stopSpeedMonitor(@NonNull Context context) {
        if (null == ISPEED) return;

        ISPEED.stopMonitorSpeed(context);
        ISPEED = null;
    }

    public boolean isNetConnected(@NonNull Context context) {
        return new Util().isNetworkAvailable(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void requestReadNetworkStats(@NonNull Context context) {
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        context.startActivity(intent);
    }

    private NetworkMonitor(){}
    private static class $ {
        private final static NetworkMonitor $$ = new NetworkMonitor();
    }

    public static NetworkMonitor getInstance() {
        return $.$$;
    }
}
